<?php

namespace App\Models;


class CtcOrdersModel
{
    /**
     * 查询当前币种正在交易数量(买)
     * entry $CtcOrdersExample
     * return int
     * */
    public static function getCtcOrdersNum($conn,$uid,$wid){
        $Sql="select count(`id`) as num from `ctc_orders` where `uid`= $uid and `wid`= $wid and `status` in(10,20)";
        return $conn->readConn($Sql)["num"];
    }
    /**
     * 查询当前币种正在交易数量(卖)
     * entry $CtcOrdersExample
     * return int
     * */
        public static function getCtcOrdersToNum($conn,$uid,$wid){
        $Sql="select count(`id`) as num from `ctc_orders` where `to_uid` = $uid and `wid` = $wid and status in(10,20)";
        return $conn->readConn($Sql)["num"];
    }
    /**
     * 查询当前交易详情
     * entry $CtcOrdersExample
     * return int
     * */
    public static function getCtcOrders($conn,$CtcOrdersExample){
        $Sql="select `id`,`sn`, `wid`,`type`, `user_id`,`uid`,`buy_name`,`buy_mobile`,`buy_white`,`to_uid`,`sell_name`,`sell_mobile`,`sell_white`,`goods_id`,`quantity`,`price`,`total_price`,`commission`,`group_id`,`payment`,`create_time`,`update_time`,`finnshed_time`,`status`,`pay_status`,`certificate`,`pay_type`,`appeal`,`mark` from `ctc_orders` where `goods_id` = $CtcOrdersExample->goods_id and (`uid` = $CtcOrdersExample->uid or `to_uid` = $CtcOrdersExample->uid) and `status` >0 limit 1";
        return $conn->readConn($Sql);
    }
    /**
     * 查询可取消订单
     * entry $CtcOrdersExample
     * return int
     * */
    public static function getCtcCancelOrders($conn,$CtcOrdersExample){
        $Sql="select `id`,`sn`, `wid`,`type`, `user_id`,`uid`,`buy_name`,`buy_mobile`,`buy_white`,`to_uid`,`sell_name`,`sell_mobile`,`sell_white`,`goods_id`,`quantity`,`price`,`total_price`,`commission`,`group_id`,`payment`,`create_time`,`update_time`,`finnshed_time`,`appeal`,`status`,`pay_status` from `ctc_orders` where `id` = $CtcOrdersExample->id and `uid` = $CtcOrdersExample->uid and `status` in(10,20) order by `create_time` desc";
        return $conn->readConn($Sql);
    }
    /**
     * 查询可取消订单
     * entry $CtcOrdersExample
     * return int
     * */
    public static function getCtcOrdersToUid($conn,$CtcOrdersExample){
        $Sql="select `id`,`sn`, `wid`,`type`, `user_id`,`uid`,`buy_name`,`buy_mobile`,`buy_white`,`to_uid`,`sell_name`,`sell_mobile`,`sell_white`,`goods_id`,`quantity`,`price`,`total_price`,`commission`,`group_id`,`payment`,`create_time`,`update_time`,`finnshed_time`,`appeal`,`status`,`pay_status` from `ctc_orders` where `id` = $CtcOrdersExample->id and `to_uid` = $CtcOrdersExample->to_uid and `status` in(10,20)";
        return $conn->readConn($Sql);
    }
    /**
     * 查询交易中订单
     * entry $CtcOrdersExample
     * return int
     * */
    public static function getCtcOrdersUidToUid($conn,$CtcOrdersExample,$identical){
        $Sql="select `id`,`sn`, `wid`,`type`, `user_id`,`uid`,`buy_name`,`buy_mobile`,`buy_white`,`to_uid`,`sell_name`,`sell_mobile`,`sell_white`,`goods_id`,`quantity`,`price`,`total_price`,`commission`,`group_id`,`payment`,`create_time`,`update_time`,`finnshed_time`,`appeal`,`status`,`pay_status` from `ctc_orders` where  (`to_uid` = $CtcOrdersExample->uid or `uid` = $CtcOrdersExample->uid) and `status` in(10,20) order by `create_time` desc limit $identical->pageStart,$identical->limit";
        return $conn->readConn($Sql,true);
    }
    /**
     * 查询交易中订单（数量）
     * entry $CtcOrdersExample
     * return int
     * */
    public static function getCtcOrdersCount($conn,$uid){
        $Sql="select count(`id`) as num from `ctc_orders` where  (`to_uid` = $uid or `uid` = $uid) and `status` in(10,20)";
        return $conn->readConn($Sql)["num"];
    }
    /**
     * 查询完成订单
     * entry $CtcOrdersExample
     * return int
     * */
    public static function getCtcOrdersUidToUidComplete($conn,$CtcOrdersExample,$identical){
        $Sql="select `id`,`sn`, `wid`,`type`, `user_id`,`uid`,`buy_name`,`buy_mobile`,`buy_white`,`to_uid`,`sell_name`,`sell_mobile`,`sell_white`,`goods_id`,`quantity`,`price`,`total_price`,`commission`,`group_id`,`payment`,`create_time`,`update_time`,`finnshed_time`,`appeal`,`status`,`pay_status` from `ctc_orders` where  (`to_uid` = $CtcOrdersExample->uid or `uid` = $CtcOrdersExample->uid) and `status` =30 order by `create_time` desc limit $identical->pageStart,$identical->limit";
        return $conn->readConn($Sql,true);
    }
    /**
     * 添加产品
     * entry $CtcOrdersExample
     * return int
     * */
    public static function addCtcOrders($conn,$CtcOrdersExample){
        $Sql ="INSERT INTO `ctc_orders`(`sn`, `wid`,`type`, `user_id`,`uid`,`buy_name`,`buy_mobile`,`buy_white`,`to_uid`,`sell_name`,`sell_mobile`,`sell_white`,`goods_id`,`quantity`,`price`,`total_price`,`commission`,`commission_type`,`group_id`,`payment`,`create_time`,`update_time`) VALUES ('$CtcOrdersExample->sn','$CtcOrdersExample->wid','$CtcOrdersExample->type','$CtcOrdersExample->user_id','$CtcOrdersExample->uid','$CtcOrdersExample->buy_name','$CtcOrdersExample->buy_mobile','$CtcOrdersExample->buy_white','$CtcOrdersExample->to_uid','$CtcOrdersExample->sell_name','$CtcOrdersExample->sell_mobile','$CtcOrdersExample->sell_white','$CtcOrdersExample->goods_id','$CtcOrdersExample->quantity','$CtcOrdersExample->price','$CtcOrdersExample->total_price','$CtcOrdersExample->commission','$CtcOrdersExample->commission_type','$CtcOrdersExample->group_id','$CtcOrdersExample->payment','$CtcOrdersExample->create_time','$CtcOrdersExample->update_time')";
        return $conn->executeConn($Sql);
    }
    /**
     * 修改店铺可添加字段
     * entry $CtcOrdersExample
     * return int
     * */
    public static function upCtcOrdersInside($conn,$CtcOrdersExample){
        $joinSql="";
        if($CtcOrdersExample->status !=null){
            $joinSql.=" `status` = '$CtcOrdersExample->status',";
        }
        if($CtcOrdersExample->appeal !=null){
            $joinSql.=" `appeal` = '$CtcOrdersExample->appeal',";
        }
        if($CtcOrdersExample->update_time !=null){
            $joinSql.=" `update_time` = '$CtcOrdersExample->update_time',";
        }
        if($CtcOrdersExample->finnshed_time !=null){
            $joinSql.=" `finnshed_time` = '$CtcOrdersExample->finnshed_time',";
        }
        if($CtcOrdersExample->certificate !=null){
            $joinSql.=" `certificate` = '$CtcOrdersExample->certificate',";
        }
        if($CtcOrdersExample->pay_status !=null){
            $joinSql.=" `pay_status` = '$CtcOrdersExample->pay_status',";
        }
        if($CtcOrdersExample->pay_type !=null){
            $joinSql.=" `pay_type` = '$CtcOrdersExample->pay_type',";
        }
        if($CtcOrdersExample->mark !=null){
            $joinSql.=" `mark` = '$CtcOrdersExample->mark',";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `ctc_orders` SET $innerJoin WHERE `id` = '$CtcOrdersExample->id'";
            return $conn->executeConn($upUserSql);
        }
    }
}